package com.pkk.alert.web.controller.system;


import static com.pkk.alert.web.config.content.ScheduleContent.CANCEL_SCHEDULE;
import static com.pkk.alert.web.config.content.ScheduleContent.SCHEDULED_FUTURE_MAP;

import com.alibaba.fastjson.JSONObject;
import com.pkk.alert.service.FeigeAccountService;
import com.pkk.alert.service.impl.HealthCheckBusinessService;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author peikunkun
 * @date 2020-08-17 11:21
 * @desc 测试类
 **/
@Slf4j
@RestController
@RequestMapping
public class ScheduleController {


  @Resource
  private FeigeAccountService feigeAccountService;
  @Autowired
  private ThreadPoolTaskScheduler threadPoolTaskScheduler;
  @Resource
  private HealthCheckBusinessService healthCheckBusinessService;


  /**
   * 定时任务信息
   *
   * @author peikunkun
   * @date 2020/11/6 0006 上午 10:38
   * @since
   */
  @RequestMapping(value = "info", method = {RequestMethod.GET, RequestMethod.POST})
  public CommonResponse<JSONObject> info() {
    final int activeCount = threadPoolTaskScheduler.getActiveCount();
    final int poolSize = threadPoolTaskScheduler.getPoolSize();

    final JSONObject threadJson = new JSONObject();
    threadJson.put("正在运行", activeCount);
    threadJson.put("核心保留", poolSize);

    final JSONObject scheduleJson = new JSONObject();
    scheduleJson.put("定时任务", SCHEDULED_FUTURE_MAP.keySet());
    scheduleJson.put("取消任务", CANCEL_SCHEDULE);

    final JSONObject result = new JSONObject();
    result.put("任务运行", threadJson);
    result.put("任务信息", scheduleJson);
    return ResponseUtil.success(result);
  }


  /**
   * 账户信息
   *
   * @return com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Param
   * @author peikunkun
   * @date 2020/12/1 0001 上午 11:14
   * @since
   */
  @RequestMapping(value = "account", method = {RequestMethod.GET, RequestMethod.POST})
  public CommonResponse<JSONObject> account() {
    return ResponseUtil.success(feigeAccountService.handleAccountUserInfo());
  }


  /**
   * 主动触发健康提醒
   *
   * @return com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Param
   * @author peikunkun
   * @date 2020/12/1 0001 下午 2:09
   * @since
   */
  @RequestMapping(value = "remind", method = {RequestMethod.GET, RequestMethod.POST})
  public CommonResponse<JSONObject> remind() {
    healthCheckBusinessService.handleCheckHealth();
    return ResponseUtil.success();
  }


}

